<template>
  <div class="import">
    <el-card>
      <h3>员工导入</h3>
      <ReadExcel :on-success="onSuccess" />
    </el-card>
  </div>
</template>

<script>
import { sysUserBatch } from '@/api/employees.js'
export default {
  name: 'Import',
  methods: {
    async onSuccess({ header, results }) {
      console.log('results', results)
      const obj = {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }
      const newArr = results.map((item) => {
        const temp = {}
        Object.keys(item).forEach((item2) => {
          temp[obj[item2]] = item[item2]
          if (obj[item2] === 'timeOfEntry' || obj[item2] === 'correctionTime') {
            const t1 = new Date(
              (item[item2] - 1) * 24 * 60 * 60 * 1000 + 8 * 60 * 60 * 1000
            )
            t1.setFullYear(t1.getFullYear() - 70)
            temp[obj[item2]] = t1
          }
        })
        return temp
      })
      console.log('newArr', newArr)
      //  导入接口
      await sysUserBatch(newArr)
      this.$message.success('导入成功')
      this.$router.push(this.$route.query.redirect || '/employees')
    }
  }
}
</script>

<style lang="scss" scoped>
.import {
  padding: 15px;
  text-align: center;
  height: 700px;
  .el-card {
    height: 100%;
  }
}
</style>
